import 'package:flutter/material.dart';
import 'package:gameplay_flutter/lib_baseview/theme/jr.dart';
import 'package:gameplay_flutter/lib_runtime/extension/string_extension/string_text.dart';
import 'package:gameplay_flutter/lib_runtime/extension/widget/widget_wrapper.dart';

import '../../../../../lib_baseview/lib_baseview.dart';
import '../../../../../lib_user/user_manager.dart';
import '../../../msg/room_msg.dart';

/// 公屏消息item builder
mixin RoomMsgItemBuilder {
  static bool _userInfoShowing = false;

  bool support(RoomMsg msg) => false;

  Widget buildItem(BuildContext context, RoomMsg msg) {
    return Container(
      padding: const EdgeInsets.symmetric(vertical: 8, horizontal: 5),
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          if (needShowSender(msg)) ...[
            buildSenderAvatar(context, msg),
            // Text('buildSenderAvatar'),
            const SizedBox(
              width: 6,
            ),
          ],
          Expanded(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              mainAxisSize: MainAxisSize.min,
              children: [
                if (needShowSender(msg)) ...[
                  buildSenderInfo(context, msg),
                  const SizedBox(
                    height: 4,
                  ),
                ],
                GestureDetector(
                    onLongPress: () {
                      // if (needShowSender(msg) && !isMineMsg(msg)) {
                      //   MessageInputOverlay.show(context, (input,
                      //       {ItemStyle itemStyle = ItemStyle.text,
                      //       String? extra,
                      //       RoomMsg? quoteMsg}) {
                      //     RoomPageModel roomPageModel =
                      //         context.read<RoomPageModel>();
                      //     roomPageModel.sendUserInputTextMessage(input,
                      //         itemStyle: itemStyle,
                      //         extra: extra,
                      //         quoteMsg: quoteMsg);
                      //   }, quoteMsg: msg);
                      // }
                    },
                    child: buildContent(context, msg)),
              ],
            ),
          ),
        ],
      ),
    );
  }

  /// 是否显示发送者信息
  bool needShowSender(RoomMsg msg) {
    if (msg.rtmMsg!.msg?.account != null) {
      String tmpStr = msg.rtmMsg!.msg!.account ?? '0';
      return (int.parse(tmpStr)) > 0;
    }
    return false;
  }

  bool isMineMsg(RoomMsg msg) =>
      (msg.chatContent?.senderUser?.id ?? 0) == UserManager.instance.currentUid;

  /// 发送者头像
  Widget buildSenderAvatar(BuildContext context, RoomMsg msg) {
    // User? user = msg.chatContent?.senderUser;
    Widget avatarWidget = GestureDetector(
      onTap: () {
        // showUserInfo(context,user: channelUser, secretUser: user.secretUser == 1)
      },
      child: msg.rtmMsg!.msg?.headPortraitsUrl == null
          ? const SizedBox()
          : JR
              .img(msg.rtmMsg!.msg!.headPortraitsUrl!, width: 30, height: 30)
              .corner(radius: 15),
    );
    // if (user?.avatarFrame.isNotEmpty ?? false) {
    //   avatarWidget = Stack(
    //     alignment: Alignment.center,
    //     children: [
    //       avatarWidget,
    //       IgnorePointer(
    //         child: JR.img(
    //           user?.avatarFrame ?? '',
    //           width: 30 * 1.3,
    //           height: 30 * 1.3,
    //         ),
    //       ),
    //     ],
    //   );
    // }
    return avatarWidget;
  }

  /// 发送者信息
  Widget buildSenderInfo(BuildContext context, RoomMsg msg) {
    // User? sender = msg.chatContent?.senderUser;

    bool roomowner =
        msg.rtmMsg?.msg?.account == (UserManager.instance.id).toString();
    return Wrap(
      crossAxisAlignment: WrapCrossAlignment.center,
      children: [
        if (roomowner)
          JR.img('room/room_mic_host_red_icon.png', width: 16, height: 16),
        ConstrainedBox(
          constraints: const BoxConstraints(maxWidth: 100),
          child: (msg.rtmMsg?.msg?.nickName ?? '').text(
              color: Colors.white, fontSize: 16, weight: TextFontWeight.medium),
        ),
      ],
    );
  }

  // Iterable<Widget> buildTags(SimpleUser? sender) {
  //   if (sender?.tags.isEmpty ?? true) return [];
  //   if (UserManager.instance.currentUser?.job == Job.BOSS) return [];
  //   return sender!.tags.map((_) => buildTagItem(_)).expand((_) => [
  //         _,
  //         const SizedBox(
  //           width: 4,
  //         )
  //       ]);
  // }

  Widget buildTagItem(String tagUrl) {
    return image(tagUrl, height: 18);
  }

  Widget buildContent(BuildContext context, RoomMsg msg);

  // static Future showUserInfo(BuildContext context,
  //     {SimpleChannelUser? user, int uid = 0, bool secretUser = false}) async {
  //   try {
  //     if (_userInfoShowing) return;
  //     _userInfoShowing = true;

  //     user ??= SimpleChannelUser.create()..id = $fixnum.Int64(uid);
  //     RootProxyModel roomModel = context.read<RootProxyModel>();
  //     if (context.mounted && user.id.toInt() > 0)
  //       await RoomPersonalDataWidget.show(context, roomModel.channel, user,
  //           secretUser: secretUser);
  //   } finally {
  //     _userInfoShowing = false;
  //   }
  // }
}
